﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Hammock;
using Hammock.Authentication.Basic;
using Hammock.Authentication.OAuth;
using System.Collections.Specialized;
namespace Project.Models
{
    public class Twitter
    {
        public string Solicitar(HttpRequestBase Request, string devolver) {
            OAuthToken token = new OAuthToken();
            OAuthCredentials credentials = new OAuthCredentials()
            {
                Type = OAuthType.RequestToken,
                SignatureMethod = OAuthSignatureMethod.HmacSha1,
                ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                ConsumerKey = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"],
                ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"]
            };
            credentials.CallbackUrl = String.Format("{0}://{1}:{2}/{3}", Request.Url.Scheme, Request.Url.Host, Request.Url.Port, devolver);




            // Use Hammock to create a rest client, specifying the authority (the base URL for requests) and the credentials we just created
            var client = new RestClient
            {
                Authority = "http://twitter.com/oauth",
                Credentials = credentials
            };

            // Use Hammock to create a request, specifiying the rest of the path (which is appended to Authority)
            var request = new RestRequest
            {
                Path = "request_token"
            };
            RestResponse response = client.Request(request);

            if (response == null)
                throw new NullReferenceException("The response returned was null.");

            if (response.StatusCode == System.Net.HttpStatusCode.OK)
            {
                System.Collections.Specialized.NameValueCollection responseCol = HttpUtility.ParseQueryString(response.Content);


                token.Token = responseCol["oauth_token"];
                token.TokenSecret = responseCol["oauth_token_secret"];
                token.CallbackConfirmed = bool.Parse(responseCol["oauth_callback_confirmed"]);


            }
            else
            {
                throw new ApplicationException("The server responded with a status code of " + response.StatusCode.ToString());
            }
            return new Uri("http://api.twitter.com/oauth/authorize?oauth_token=" + token.Token).AbsoluteUri;
        }
        public NameValueCollection Recibir(HttpRequestBase Request) 
        {
            string token = Request["oauth_token"];
            string verifier = Request["oauth_verifier"];

            OAuthCredentials credentials = new OAuthCredentials()
            {
                Type = OAuthType.AccessToken,
                SignatureMethod = OAuthSignatureMethod.HmacSha1,
                ParameterHandling = OAuthParameterHandling.HttpAuthorizationHeader,
                ConsumerKey = System.Configuration.ConfigurationManager.AppSettings["ConsumerKey"],
                ConsumerSecret = System.Configuration.ConfigurationManager.AppSettings["ConsumerSecret"],
                Token = token,
                Verifier = verifier,
                Version = "1"
            };

            var client = new RestClient
            {
                Authority = "http://twitter.com/oauth",
                Credentials = credentials
            };

            var request = new RestRequest
            {
                Path = "access_token"
            };
            var response = client.Request(request);

            if (response == null)
                throw new ArgumentNullException("response", "The REST response cannot be null");

            return HttpUtility.ParseQueryString(response.Content);
        }
    }
}